package cn.iune.erp.core;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.JSONObject;

public class BusinessService {
	
	////////////////////////////////////////////////////////////////////////////////////

	//计税
	
	public static JSONObject taxCalculate(BigDecimal itemAmount,BigDecimal itemPrice,int taxType,BigDecimal taxRate,BigDecimal discountRate){
		
		BigDecimal orderAmount = itemAmount.multiply(itemPrice).multiply(discountRate);
		BigDecimal orderAmountOld = BigDecimal.ZERO;
		BigDecimal orderAmountTax = BigDecimal.ZERO;
		BigDecimal orderAmountTotal = BigDecimal.ZERO;
		
		//////////////////////////////////////////////////////////////////////////////////////////////////////////
		if (taxType == -1) {
			// -1.免税
			orderAmountOld = orderAmount;
			orderAmountTax = BigDecimal.ZERO;
		}
		if (taxType == 0) {
			// 0.应税外加
			orderAmountOld = orderAmount;
			orderAmountTax = orderAmountOld.multiply(taxRate).setScale(2, BigDecimal.ROUND_HALF_UP);;
		}
		if (taxType == 1) {
			// 1.应税内含
			orderAmountOld = orderAmount.divide(taxRate.add(BigDecimal.ONE), 2, BigDecimal.ROUND_HALF_UP);
			orderAmountTax = orderAmount.subtract(orderAmountOld);
		}
		orderAmountTotal = orderAmountOld.add(orderAmountTax);
		///////////////////////////////////////////////////////////////////////////////////////////////////////////
		
		//取两位小数
		orderAmount = orderAmount.setScale(2, BigDecimal.ROUND_HALF_UP);
		orderAmountOld = orderAmountOld.setScale(2, BigDecimal.ROUND_HALF_UP);
		orderAmountTax = orderAmountTax.setScale(2, BigDecimal.ROUND_HALF_UP);
//		orderAmountTotal = orderAmountTotal.setScale(2, BigDecimal.ROUND_HALF_UP);
		
		JSONObject json = new JSONObject();
		json.put("orderAmount", orderAmount);
		json.put("orderAmountOld", orderAmountOld);
		json.put("orderAmountTax", orderAmountTax);
		json.put("orderAmountTotal", orderAmountTotal);
		
		return json;
	}
	
	
	
	//计税
	public static List<BigDecimal> taxCalculate(BigDecimal itemAmount,BigDecimal itemPrice,BigDecimal discountRate,BigDecimal discountAmount,int taxType,BigDecimal taxRate){
		//输入参数：数量，单价，税种，税率
		//输出结果：金额，未税金额，税额，价税合计
		/////////////////////////////////////////////////////////////////////////
		//金额（不分正负），计价数量会影响
		BigDecimal orderAmount = itemAmount.multiply(itemPrice).multiply(discountRate);
		orderAmount = orderAmount.subtract(discountAmount);
				
		BigDecimal orderAmountOld = BigDecimal.ZERO;
		BigDecimal orderAmountTax = BigDecimal.ZERO;
		BigDecimal orderAmountTotal = BigDecimal.ZERO;
		//2、根据单价是否含税的情况，计算出未税金额和税额；

		//////////////////////////////////////////////////////////////////////////////////////////////////////////
		if (taxType == -1) {
			// -1.免税
			orderAmountOld = orderAmount;
			orderAmountTax = BigDecimal.ZERO;
		}
		if (taxType == 0) {
			// 0.应税外加
			orderAmountOld = orderAmount;
			orderAmountTax = orderAmountOld.multiply(taxRate).setScale(2, BigDecimal.ROUND_HALF_UP);;
		}
		if (taxType == 1) {
			// 1.应税内含
			orderAmountOld = orderAmount.divide(taxRate.add(BigDecimal.ONE), 2, BigDecimal.ROUND_HALF_UP);
			orderAmountTax = orderAmount.subtract(orderAmountOld);
		}
		orderAmountTotal = orderAmountOld.add(orderAmountTax);
		///////////////////////////////////////////////////////////////////////////////////////////////////////////
		
		//取两位小数
		orderAmount = orderAmount.setScale(2, BigDecimal.ROUND_HALF_UP);
		orderAmountOld = orderAmountOld.setScale(2, BigDecimal.ROUND_HALF_UP);
		orderAmountTax = orderAmountTax.setScale(2, BigDecimal.ROUND_HALF_UP);
//		orderAmountTotal = orderAmountTotal.setScale(2, BigDecimal.ROUND_HALF_UP);
		
		List<BigDecimal> list = new ArrayList<BigDecimal>();
		list.add(orderAmount);
		list.add(orderAmountOld);
		list.add(orderAmountTax);
		list.add(orderAmountTotal);
		
		return list;
	}
	
	
	/*
	public static String checkoutTimesAddOne(String checkoutTimes){
		String checkoutTimes_s = checkoutTimes;
		if(null==checkoutTimes_s || "".equals(checkoutTimes_s)){
			checkoutTimes_s = "00000";
		}
		int checkoutTimes_i = Integer.parseInt(checkoutTimes_s);
		checkoutTimes_i++;
		checkoutTimes_s = String.format("%05d", checkoutTimes_i);
		return checkoutTimes_s;
	}

	public static String checkoutTimesSubOne(String checkoutTimes){
		String checkoutTimes_s = checkoutTimes;
		if(null==checkoutTimes_s || "".equals(checkoutTimes_s)){
			checkoutTimes_s = "00000";
		}
		int checkoutTimes_i = Integer.parseInt(checkoutTimes_s);
		checkoutTimes_i--;
		checkoutTimes_s = String.format("%05d", checkoutTimes_i);
		return checkoutTimes_s;
	}
	*/


}
